<?php

  /**
   * @author Dany Mottier
   * @version 1.0
   */
  class StreamWriter extends Base {

    /** @var string The name of the file to work with. */
    private $filename;
    /** @var resource The handle of the file when it's open. */
    private $handle;
    /** @var string The line terminator string used by the current text writer. */
    private $endline = "\r\n";

    /**
     * Open the file specified in parameter.<br />
     *
     * @param string $filename The name of the file to work with.
     */
    public function __construct($filename) {
      if (is_string($filename)) {
        $this->filename = $filename;
        $this->handle = fopen($filename, "w+");
      }
      else
        throw new IllegalArgumentException("\$filename parameter should be a string");
    }

    /**
     * Close the handle on object destruction.<br />
     *
     * @link http://be.php.net/manual/en/function.is-null.php
     */
    public function __destruct() {
      if (!is_null($this->handle))
        fclose($this->handle);
    }

    /**
     * Sets the line terminator string used by the current text writer.<br />
     *
     * @param string $newline The line terminator currently used.
     */
    public function setNewLine($newline) {
      if (is_string($newline)) {
        $this->endline = $newline;
      } else {
        throw new IllegalArgumentException("\$newline parameter should be a string");
      }
    }

    /**
     * Gets the line terminator string used by the current text writer.<br />
     *
     * @return string The line terminator currently used.
     */
    public function getNewLine() {
      return $this->endline;
    }

    /**
     * Close the open handle of the file.<br />
     *
     * @return boolean Return TRUE on succes or FALSE on failure.<br /><br />
     *
     * @link http://be.php.net/manual/en/function.fclose.php
     */
    public function close() {
      $return = fclose($this->handle);

      if ($return) {
        $this->handle = NULL;
      }

      return $return;
    }

    /**
     * Writes the contents of $string to the open file.<br />
     *
     * @param string $string The string to be written.
     *
     * @return mixed Returns the number of bytes written, or FALSE on error.<br /><br />
     *
     * @link http://be.php.net/manual/en/function.is-string.php
     * @link http://be.php.net/manual/en/function.fwrite.php
     */
    public function write($string) {
      if (is_string($string))
        return fwrite($this->handle, $string);
      else
        throw new IllegalArgumentException("\$string parameter should be a string");
    }

    /**
     * Writes the contents of $string terminated by a CR LF (or StreamWriter::$endline) to the open file.<br />
     *
     * @param string $string The string to be written.
     *
     * @return mixed Returns the number of bytes written, or FALSE on error.<br /><br />
     *
     * @link http://be.php.net/manual/en/function.is-string.php
     * @link http://be.php.net/manual/en/function.fwrite.php
     */
    public function writeLine($string) {
      if (is_string($string))
        return fwrite($this->handle, $string . $this->endline);
      else
        throw new IllegalArgumentException("\$string parameter should be a string");
    }

    /**
     * Write a string to a file.<br />
     *
     * @param string $filename The filename to write.
     * @param mixed $data This could be a string, an array or a resource.
     *
     * @return mixed The number of bytes that were written to the file, or FALSE on failure.<br /><br />
     *
     * @link http://be.php.net/manual/en/function.is-string.php
     * @link http://be.php.net/manual/en/function.file-put-contents.php
     */
    public static function writeToFile($filename, $data) {
      if (is_string($filename)) {
        if (is_string($data)) {
          return file_put_contents($filename, $data);
        } else {
          throw new IllegalArgumentException("\$data parameter should be a string");
        }
      } else {
        throw new IllegalArgumentException("\$filename parameter should be a string");
      }
    }

  }

?>
